<?php

class LanguageExperiencesController extends AppController {
    
    var $helpers = array('Html', 'Form', 'Javascript');
    var $components = array('RequestHandler');
    var $name = 'Languages';

    var $uses = array('LanguageExperience', 'Language', 'LanguageLevel'); // specifico i modelli che utilizza il controller
    
    function isAuthorized() {
        return true;
    }
    
    function index($person_id='', $from = '') {       
        
        $params = array(
                    'conditions' => array('LanguageExperience.person_id' => $person_id)
                );
        $this->set('languageExperiences', $this->LanguageExperience->find('all', $params));
        $this->set('person', $this->LanguageExperience->Person->findById($person_id));
        $this->set('from', $from);
    }
    
    function add($person_id='') {
        
        if(!empty($this->data) && $this->LanguageExperience->validates()){

            if($this->LanguageExperience->save($this->data)) {
                $this->Session->setFlash('L\'esperienza &egrave; stata salvata');
                $this->redirect(array('action' => 'index', $this->data['LanguageExperience']['person_id']));
            }
        }
        
        // preparo array delle opzioni per le select del form
        $this->set('language_levels', $this->LanguageLevel->find('list'));
        $this->set('languages', $this->Language->find('list'));
        $this->set('person_id', $person_id);
        
    }
    
    function edit($id) {
        // if $_POST is empty read the information from DB
         if(empty($this->data)) {
             $this->LanguageExperience->id = $id;
             $this->data = $this->LanguageExperience->read();
             
             if($this->data['LanguageExperience']['mother_language'] == '1')
                 $checked = 'checked';
             else
                 $checked = '';
             // preparo array delle opzioni per le select del form
             $this->set('language_levels', $this->LanguageLevel->find('list'));
             $this->set('languages', $this->Language->find('list'));
             $this->set('checked', $checked);
         }
         // if $_POST != empty save the information
         else {
             if($this->LanguageExperience->save($this->data)) {
                 $this->Session->setFlash("L'esperienza è stata aggiornata");
                 $this->redirect(array('action' => 'index', $this->data['LanguageExperience']['person_id']));
             }
         }
    }
    
    function delete($id, $person_id) {
        $this->LanguageExperience->delete($id);
        $this->Session->setFlash('L\'esperienza è stata eliminata');
        $this->redirect(array('action' => 'index', $person_id));
    }
    
    /*
     * Add a new language to the languages table. AJAX call.
     * 
     * @return string New language id
     */
    function add_language() {
        
        $this->autoRender = false;
        
        if($this->RequestHandler->isAjax()) {
            $this->Language->save($this->params['form']);
            echo $this->Language->id;
        }
        
    }
}

?>
